1 00:00:00,700 --> 00:00:01,720 Welcome back. 2 00:00:01,720 --> 00:00:06,610 One module that we can script real quick in this lecture is going to be our lightning service. 3 00:00:06,610 --> 00:00:12,910 All the service is going to do is continually loop and randomly fire an event to clients to summon lightning. 4 00:00:12,910 --> 00:00:16,900 So that means the only thing that we're going to need in here is replicated storage. 5 00:00:16,960 --> 00:00:22,990 To make a reference to the game comms event, we can quickly create our table. 6 00:00:23,800 --> 00:00:28,990 That will represent our service, and we'll make sure to return it at the end of our script here. 7 00:00:30,090 --> 00:00:34,470 And then we can make a reference to our game comms event. 8 00:00:36,270 --> 00:00:38,010 That remotes game communication. 9 00:00:38,010 --> 00:00:41,010 And then we can also get our game comms enum. 10 00:00:41,010 --> 00:00:46,500 So we'll require from upgraded storage dot modules, dot enums, dot game communication enum. 11 00:00:46,710 --> 00:00:49,680 And then we're also going to need a random data type. 12 00:00:50,890 --> 00:00:55,210 Then we can just have a start function inside of our lightning service. 13 00:00:56,080 --> 00:01:02,170 And all this is going to do is it's going to spawn a while loop in a new thread so we can use task dot 14 00:01:02,170 --> 00:01:05,140 defer because we don't need it to run instantaneously. 15 00:01:05,350 --> 00:01:08,320 And we'll just have a loop that runs forever. 16 00:01:08,530 --> 00:01:15,250 And all we're going to do is we're going to use our RNG next integer function, pass a number of like 17 00:01:15,250 --> 00:01:17,410 something between 1 and 35. 18 00:01:17,410 --> 00:01:24,460 And if that's equal to one, then we can use the game comms event and fire to all of the clients in 19 00:01:24,460 --> 00:01:27,790 our game to summon lightning. 20 00:01:27,790 --> 00:01:31,960 So game comms enum dot two client dot lightning. 21 00:01:31,960 --> 00:01:37,990 And then just to make sure that lightning doesn't fire too often after we fire to all the clients, 22 00:01:37,990 --> 00:01:40,240 we'll just wait something like three seconds. 23 00:01:40,540 --> 00:01:45,040 And then this whole while loop itself will run every single second. 24 00:01:45,040 --> 00:01:48,400 So we'll have a yield for one second, and then it'll do the check. 25 00:01:48,400 --> 00:01:53,620 And then that means lightning should fire maybe once about every 35 seconds. 26 00:01:53,620 --> 00:01:56,050 And that's all we need to do for this service. 27 00:01:56,050 --> 00:01:58,450 Now to actually see lightning inside of our game. 28 00:01:58,450 --> 00:02:01,270 Let's go ahead and reset the brightness back to be zero. 29 00:02:01,270 --> 00:02:06,010 And let's go ahead and enable fog to its original state of 135. 30 00:02:06,010 --> 00:02:08,080 And let's go ahead and test out our lightning. 31 00:02:09,770 --> 00:02:12,500 So at some point we should have lightning. 32 00:02:12,530 --> 00:02:13,280 There we go. 33 00:02:14,120 --> 00:02:15,440 And there's our lightning. 34 00:02:15,440 --> 00:02:20,990 So we further added to the ambiance in our game where the brightness in the workspace increases and 35 00:02:20,990 --> 00:02:23,090 we get some nice lightning sound effects. 36 00:02:23,540 --> 00:02:28,070 And because we're looping it from the server, that means all of the clients in our game will have lightning 37 00:02:28,070 --> 00:02:31,190 effects applied at the exact same time as each other. 38 00:02:31,280 --> 00:02:34,040 Let's see if we can get another lightning strike here. 39 00:02:37,180 --> 00:02:38,470 And there we go. 40 00:02:38,650 --> 00:02:41,230 So looks like our ambience is coming together. 41 00:02:41,230 --> 00:02:41,770 Great. 42 00:02:41,770 --> 00:02:45,490 That's all for me in this lecture, and I will see you in the next one.